#!/usr/bin/env xonsh
from user_action_log import user_action_log
from os.path import exists
from concurrent.futures import ThreadPoolExecutor


APP_GZ = dict(
gb = (670104, 670107, 670106),
fm = (670110,),
plus = (670108,),
yo = (670109,)
)

# for k,v in APP_GZ.items():
#   print(f"mkdir -p ~/app/{k}/;mv ~/old/{k}/{v}.json.gz ~/app/{k}/")
# # raise
APP_KEY = dict(
gb="""09efa8a1c4739f85
12c4ff7a285ba9ae
1960752d3a819cda
20242b82592caa52
26c8c2646c1a476d
2d637122faaa753c
341cfe669e424eae
3ae7d75d0ff511f1
4192d91e5de4ebb2
4859fc14d141afe6
4f12ede7b103232c
55ca730ce98eb258
5c784d6cd3aed27f
634315fe8a06cf35
6a0bc62f4576dad8
70b7272898f04bac
776c349a632beb91
7e3134c8f68fd265
84f5473fed25a523
8ba1fb13fdccafa0
926e219558ce1a55
992f9689672d1418
9fe8de23ff9fb9c9
a6a3123732c167c1
ad3e973b19ec8ce3
b3fc8b86c89e50fa
baadb33bd5d55386
c154df3bc8cdbc42
c829fc589e370149
cede6fe46e768c52
d58d8c54390385de
dc3b165ad83b6262
e2f9fb85b6be8382
e9a99bf0ae595057
f07f1869af5147cc
f72bc60b7856507b
fdce05998968f553
1c62e31230c9ac17
36186ad8f2812904
4ea25c27551b651e
6d55386f27d1956d
83f3f366a82a94a8
9a185a6651c2be6d
b32f90f1dfd3da17
ca8caed240539f5e
e25b0d89e823cfca
fe4edaca2d69276""",
yo="""3dfd443539af88e7
696ffcd1efbfe53f
9050abb8f14fc541
905e4c9adb56b396
90b2e0c7d8248e3f
91864d76c663a909
000a8b2a4308b1bf
53ae1618aef1e6e6
5d845c2744ea01ba
5d858a56cc6c6b8a
6a450897eb8ae99a
71faced3065bec2c
7206757b45df054d
8fba67b4f4240e61
a5cbcd17c928b924
d5c1be561e3a3de3""",
fm ="""0f167c7cbc19ac6f
1c433bdff910cf3
28f65426655e5aae
357f621d1df1920c
41b7526d04235045
4eb6e0377dfd0e50
5b9f256d2f2b51aa
67dc3fa4a2862a56
7449e38e6379543a
81121ce0f8f74d94
8d753820a5eba74b
99da83f60515483d
a68faa060b0ef719
b3964d551b89b3ab
c077d46b5c09f365
cd2161ac12f8a578
d92f024f76327be9
e5ceed10295e2b17
f235f14bb012fd68
fe89fac0181bc6c3""",
plus="""2c4c2441ae728d6c
576e570195490b2e
7e86d40b3529ac73
a86d9b0a02444f34
ca14468c449b7806
f2d02f571baf3711
000227c17461712b
00123a693c2a6702
32fd6668d243c7b9
6ff85b235bd7fe46
7bd4a5f88fe61c5
97dac4c2af142dc0
a3ed68c995363c3c
b9dd09967e549f78
b9e18f4e57f845b8
d6dcd80cefd41f26"""
)

def try_except(func):
  def _(*args,**kwds):
    try:
      return func(*args, **kwds)
    except:
      import traceback
      traceback.print_exc()
  return _

#@try_except
def rg(fdir, gz, aid_li):
  fpath = f"{fdir}/{gz}.json.gz"
  rgpath = fpath[:-7]+"rg"
  print(fpath)
  if exists(rgpath):
    print(f"{rgpath} exist")
  else:
    ls -alh @(fpath)
    aid = "|".join(aid_li)
    print(rgpath)
    zcat @(fpath)|rg @(aid) > @(rgpath).ing
    mv @(rgpath).ing @(rgpath)
  user_action_log(rgpath)

def main():
  with ThreadPoolExecutor(max_workers=8) as executor:
    for app,key_li in APP_KEY.items():
      aid_li = tuple(filter(bool,(i.strip() for i in key_li.split("\n"))))
      # print(app,len(aid_li))
      if app not in APP_GZ:
        print(f'app {app} not in APP_GZ')
        continue
      fdir = f"/root/app/{app}"
#ls -alh @(fdir)
      for gz in APP_GZ[app]:
        executor.submit(rg,fdir,gz,aid_li)
main()
rsync -av /root/user r5:/root/zst/

